<?php
namespace Framework\Library;

class Validation extends \Phalcon\Validation
{
    private $paramRules;
    private $platformValidatorNamespace;

    public function __construct($paramRules, $platformValidatorNamespace = null)
    {
        $this->paramRules = $paramRules;
        $this->platformValidatorNamespace = $platformValidatorNamespace;
        parent::__construct();
    }

    public function initialize()
    {
        foreach ($this->paramRules as $param => $rule) {
            if ($this->platformValidatorNamespace) {
                $className = '\\' . $this->platformValidatorNamespace . '\\' . ucfirst($rule['type']) . 'Validator';
                if (class_exists($className)) {
                    $this->add($param, new $className($rule));
                    continue;
                }
            }

            $className = '\Framework\Library\Validator\\' . ucfirst($rule['type']) . 'Validator';
            $this->add($param, new $className($rule));
        }
    }
}
